package com.example.obs.utils;

import com.example.obs.vo.ResultVO;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.http.MediaType;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.Charset;

/**
 * @author Kevin
 * @date 2020-06-13 9:44
 */
public class ResponseUtil {

    private static ObjectMapper objectMapper = new ObjectMapper();

    public static void response(ResultVO resultVO, HttpServletResponse response, int status) {
        // 解决中文乱码
        response.setCharacterEncoding(Charset.defaultCharset().displayName());
        response.addHeader("Content-Type", MediaType.APPLICATION_JSON_VALUE);
        String result = null;
        try {
            result = objectMapper.writeValueAsString(resultVO);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        try {
            response.getWriter().write(result);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
